package gui.action;

import gui.entity.AvatarEntity;
import gui.entity.UserEntity;

import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.actions.SelectionProviderAction;

import vworld.Client;

public class KickAction extends SelectionProviderAction {
	public final static String ID = "gui.action.KickAction";

	public KickAction(final ISelectionProvider provider, final String text) {
		super(provider, text);
		setId(ID);
		setText("&Kick");
		setToolTipText("Kick the selected avatar or user.");
	}

	public void run() {
		IStructuredSelection structuredSelection = getStructuredSelection();
		Object firstElement = structuredSelection.getFirstElement();
		kick(firstElement);
	}

	public static boolean kick(final Object object) {
		if (object instanceof AvatarEntity) {
			AvatarEntity avatarEntityToKick = (AvatarEntity) object;
			Client.ADMIN_SERVICE.kickAvatar(avatarEntityToKick.getAvatar());
			return true;
		}

		if (object instanceof UserEntity) {
			UserEntity userEntityToKick = (UserEntity) object;
			Client.ADMIN_SERVICE.kickUser(userEntityToKick.getUser());
			return true;
		}
		return false;
	}
}